﻿package org.nickro.project.utils {

    public class Numbers {

        public static const ROUND_DOWN:int = 0;
        public static const ROUND_UP:int = 2;
        public static const ROUND_NEAREST:int = 1;

        public function Numbers(){
            throw (new Error("ERROR com.tobyfreeman.utils.Numbers constructor: Class never intended to be instantiated"));
        }
		/**
		 * 格式化浮点数，保留几位小数
		 * @param	n
		 * @param	length
		 * @return
		 */
		public static function formatFloatLength(n:Number, length:int):Number {
			var l:Number = Math.pow(10, length);
			return (int(n * l) / l);
		}
		/**
		 * 数字转换成以千为单位的字符串
		 * @param	n
		 * @param	separator
		 * @param	"
		 * @return
		 */
        public static function formatIntThousands(n:int, separator:String=","):String{
            var group:String;
            var groupLen:int;
            var s:String = n.toString();
            var len:int = s.length;
            var sOut:String = "";
            var i:int;
            do  {
                i = (i + 3);
                groupLen = ((i)>len) ? (3 - (i - len)) : 3;
                group = s.substr(-(i), groupLen);
                if ((!(i == 3)) && (!(group == "-"))){
                    sOut = (separator + sOut);
                };
                sOut = (group + sOut);
            } while (i < len);
            return (sOut);
        }
        public static function formatDecPlaces(n:Number, places:int, rounding:int=1, dropZeroes:Boolean=false):String{
            if (places <= 0){
                return (String(round(n, rounding)));
            };
            if (n != 0){
                n = (n * Math.pow(10, places));
                n = (round(n, rounding) * Math.pow(10, -(places)));
            };
            var s:String = String(n);
            var pointIndex:int = s.indexOf(".");
            if (dropZeroes){
                if (pointIndex != -1){
                    s = s.substr(0, ((pointIndex + places) + 1));
                };
            } else {
                if (pointIndex == -1){
                    pointIndex = s.length;
                    s = (s + ".");
                };
                s = Strings.padTrimEnd(s, "0", ((pointIndex + places) + 1));
            };
            return (s);
        }
		/**
		 * 取整
		 * @param	n
		 * @param	rounding	舍去类型: 0 下 1 四舍五入 2 上
		 * @return	
		 */
        public static function round(n:Number, rounding:int):int{
            switch (rounding){
                case ROUND_DOWN:
                    return (Math.floor(n));
                case ROUND_NEAREST:
                    return (Math.round(n));
                case ROUND_UP:
                    return (Math.ceil(n));
                default:
                    trace((("/error/ Invalid rounding in Numbers.round: '" + rounding) + "'"));
                    return (n);
            };
        }
		/**
		 * 
		 * @param	n
		 * @param	Min
		 * @param	Max
		 * @return
		 */
        public static function wrapInt(n:Number, Min:Number, Max:Number):Number{
            while (n < Min) {
                n = ((n - Min) + (Max + 1));
            };
            while (n > Max) {
                n = ((n - Max) + (Min - 1));
            };
            return (n);
        }  
    }
}
